"""
python的对象是存放在字典里面，且存放的形式和json非常类似，所以有个包专门实现python对象和json对象的互相转换
"""

#python对象转json dump
import json
person = {'name':'张三','age':'18','gender':'male','isPlayer':True}
print('----------------')
print(person)
print(type(person))
print('----------------')
write_path = 'tt.json'
jsonStr = json.dumps(person)
print(jsonStr)
print(type(jsonStr))
with open(write_path,'w',encoding='utf-8') as f:
    json.dump(person,f,ensure_ascii=False,indent=4)


#json对象转python laods
print('-------------------------------------------------')
aa = '{"name": "张三", "age": "18", "gender": "male", "isPlayer": true}'
print(aa)
pythonObj = json.loads(aa)
print(pythonObj)
print(type(pythonObj))
print(pythonObj['name'])

pythonObj2 = json.load(open('tt.json','r',encoding='utf-8'))
print(pythonObj2)
print(type(pythonObj2))


